import { createRouter, createWebHashHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { useSystemStore } from '@/stores/system'

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/Order',
      name: 'Order',
      meta: {
        needLogin: true
      },
      component: () => import('@/views/Order.vue')
    },
    {
      path: '/Weight',
      name: 'Weight',
      meta: {
        needLogin: true
      },
      component: () => import('@/views/Weight.vue')
    },
    {
      path: '/WeightSuccess',
      name: 'WeightSuccess',
      meta: {
        needLogin: true
      },
      component: () => import('@/views/WeightSuccess.vue')
    },
    {
      path: '/OrderDetail',
      name: 'OrderDetail',
      meta: {
        needLogin: true
      },
      component: () => import('@/views/OrderDetail.vue')
    },
    {
      path: '/Print',
      name: 'Print',
      meta: {
        needLogin: true
      },
      component: () => import('@/views/Print.vue')
    },
    {
      path: '/Settings',
      name: 'Settings',
      meta: {
        needLogin: false
      },
      component: () => import('@/views/Settings.vue')
    },
    {
      path: '/AndroidConfig',
      name: 'AndroidConfig',
      meta: {
        needLogin: true
      },
      component: () => import('@/views/AndroidConfig.vue')
    },
    {
      path: '/Confirm',
      name: 'Confirm',
      meta: {
        needLogin: true
      },
      component: () => import('@/views/Confirm.vue')
    },
    {
      path: '/Cus',
      name: 'Cus',
      meta: {
        needLogin: true
      },
      component: () => import('@/views/Cus.vue')
    },
    {
      path: '/CusDetail',
      name: 'CusDetail',
      meta: {
        needLogin: true
      },
      component: () => import('@/views/CusDetail.vue')
    }
  ]
})
router.beforeEach(async (to) => {
  if (!to.meta.needLogin) {
    return true
  }
  const userStore = useUserStore()
  if (!userStore.token) {
    await userStore.login()
  }
  const systemStore = useSystemStore()
  const promiseArr = []
  if (!systemStore.aliOssSignature.signature) {
    promiseArr.push(systemStore.getAliOssSignature())
  }
  if (!systemStore.displayWeightType) {
    promiseArr.push(systemStore.getDisplayWeightType(userStore.user.companyId))
  }
  if (!systemStore.defaultMaterialSh.warehouseId) {
    promiseArr.push(systemStore.getDefaultMaterial(userStore.user.companyId))
  }
  await Promise.all(promiseArr)
})
export default router
